/**
 * Created by yongwang on 2015/9/2.
 * 左右选择框
 */
(function(w){
    var larSelect = function(){
        this.init();
    },c;
    larSelect.prototype = {
        init: function(){
            c = this;
            //c.paint();
            c.evt();
        },
        paint: function(main){
            c.main = main;
            var html = "<table border='0'>"+
                "<tr align='center'>"+
                "<td>"+
                "<select class='lists_select' multiple='multiple' ng-model='oneCommunity'>"+
                "</select>"+
                "</td>"+
                "<td>"+
                "<input type='button' value='>>' class='addAllList' /><br /><br />"+
                "<input type='button' value='>' class='addList'/><br /><br />"+
                "<input type='button' value='<' class='removeList'/><br /><br />"+
                "<input type='button' value='<<' class='removeAllList'/>"+
                "</td>"+
                "<td>"+
                "<select class='lists_selected' multiple='multiple' ng-model='crule.community_id'></select>"+
                "</td>"+
                "</tr>"+
                "</table>";
            c.main.html(html);
        },
        addRole: function(data){
            var html = "";
            data.forEach(function(item){
                html += "<option value='"+item.roleID+"' style='height: 20px;'>"+item.roleName+"</option>";
            });
            $(".lists_select").html(html);
        },
        editRole: function(allData,data){
            var leftHtml = "",rightHtml = "",leftArray = [],rightArray = [];
            allData.forEach(function(item,index){
                var isHas = false;
                data.forEach(function(it){
                    if(item.roleID == it.roleID) isHas = true;
                });
                if(isHas)rightArray.push(item);else leftArray.push(item);
                isHas = false;
            });
            leftArray.forEach(function(item){
                leftHtml += "<option value='"+item.roleID+"' style='height: 20px;'>"+item.roleName+"</option>";
            });
            rightArray.forEach(function(item){
                rightHtml += "<option value='"+item.roleID+"' style='height: 20px;'>"+item.roleName+"</option>";
            });

            $(".lists_select").html(leftHtml);
            $(".lists_selected").html(rightHtml);
        },
        evt: function(){
            //全部移到右边
            $(document).on("click",".addAllList",function(){
                var p = this.parentNode.parentNode;
                //获取全部的选项,删除并追加给对方
                $(p).find('.lists_select option').appendTo('.lists_selected');
            });

            //全部移到左边
            $(document).on("click",".removeAllList",function(){
                var p = this.parentNode.parentNode;
                //获取全部的选项,删除并追加给对方
                $(p).find('.lists_selected option').appendTo('.lists_select');
            });
            //移到右边
            $(document).on("click",".addList",function(){
                var p = this.parentNode.parentNode;
                //先判断是否有选中
                if (!$(p).find(".lists_select option").is(":selected")) {
                    alert("请选择需要移动的选项");
                }
                //获取选中的选项，删除并追加给对方
                else {
                    $(p).find('.lists_select option:selected').appendTo('.lists_selected');
                }
            });

            //移到左边
            $(document).on("click",".removeList",function(){
                var p = this.parentNode.parentNode;
                //先判断是否有选中
                if (!$(p).find(".lists_selected option").is(":selected")) {
                    alert("请选择需要移动的选项");
                }
                //获取选中的选项，删除并追加给对方
                else {
                    $(p).find('.lists_selected option:selected').appendTo('.lists_select');
                }
            });

            //双击
        }
    };
    gObj.larSelect = larSelect;
}(window));